$(function() {
	//设置活动按钮
	changeHeaderMenu(0);
	changeMenu(0, 0);
	//初始化列表数据
	initPoint();
});

/**
 * 初始化表格数据
 */
function initTable() {
	var data_sn = getUrlParam("sn");
	if(!data_sn){
		alert("系统异常,请稍候再试!");
		location.href = "inCafeList.jsp";
	}

	$.getJSON('/fw/agent/agentDev_getlist.action', {
		'sn': data_sn
	},function(data){
		if(data.success && data.result)
			data = data.result;
		data = data.rows[0];
		$('.name').html(data.name);
		$('.sn').html(data.fwsn);
		$('.addr').html(data.addr);
		$('.endTime').html(data.expireTime);
	});

	// 获取主机数
	my_bind_check_num($('.host_allowed'));
	$('.host_allowed').bind('input propertychange', function() {
		var host_allowed = $(this).val();
		setPointSelect(host_allowed);
	});
	
	$.getJSON('/fw/agent/deviceAuz_getHostAllowed.action', {
		'sn': data_sn
	},function(data){
		if(data.success && data.result)
			data = data.result;
		if(!data)
			data = 200;
		$('.host_allowed').val(data);
		setPointSelect(data);
	});
	
	$('.needPoint').html("请先选择续时时长");
}

var config = {
	agentBase: 600
}

function initPointSelect() {
	var agentBase = config.agentBase;
	var html = '';
	var p1 = parseInt(agentBase/12 * 1 * 1.5);
	html += '<option class="t" value="1" point="' + p1 + '">1个月（' + p1 + '点）</option>';
	var p2 = parseInt(agentBase/12 * 3 * 1.3);
	html += '<option class="t" value="3" point="' + p2 + '">3个月（' + p2 + '点）</option>';
	var p3 = parseInt(agentBase/12 * 6 * 1.1);
	html += '<option class="t" value="6" point="' + p3 + '">6个月（' + p3 + '点）</option>';
	html += '<option class="t" value="12" point="' + agentBase + '">1年（' + agentBase + '点）</option>';
	html += '<option class="t" value="24" point="' + (agentBase*2) + '">2年（' + agentBase*2 + '点）</option>';
	$('.timeLong').append(html);
	$(".timeLong").val(12).trigger('change');
}

function setPointSelect(host_allowed) {
	var agentBase = config.agentBase;
	var add = 0;
	host_allowed = parseInt(host_allowed);
	if(host_allowed >= 300) {
		add = Math.floor((host_allowed - 300) / 100);
		if(host_allowed % 100 >= 50)
			add += 1;
//		add = Math.floor(agentBase / 3 * add);
		add = 240 * add;
	}
	$('.timeLong option').each(function(i, obj) {
		var v = $(obj).val();
		var p;
		var t;
		if(v == 1) {
			p = parseInt((add + agentBase)/12 * 1 * 1.5);
			t = '1个月（' + p + '点）';
		} else if(v == 3) {
			p = parseInt((add + agentBase)/12 * 3 * 1.3);
			t = '3个月（' + p + '点）';
		} else if(v == 6) {
			p = parseInt((add + agentBase)/12 * 6 * 1.1);
			t = '6个月（' + p + '点）';
		} else if(v == 12) {
			p = add + agentBase;
			t = '1年（' + p + '点）';
		} else if(v == 24) {
			p = (add + agentBase) * 2;
			t = '2年（' + p + '点）';
		} else {
			return;
		}
		$(obj).attr('point', p);
		$(obj).html(t);
	});
	$(".timeLong").trigger('change');
}

function initPoint() {
	$(".timeLong").change(function(){
		var point = $(".timeLong option:selected").attr('point');
		if(point)
			$('.needPoint').html(point + "点数");
		else
			$('.needPoint').html("请先选择续时时长");
	});
	$.getJSON('/fw/agent/agentbaseconfig_getbyid.action', function(data) {
		if(data.success && data.result) {
			var agentBase = data.result?data.result:600;
			agentBase = parseInt(agentBase);
			config.agentBase = agentBase;
			initPointSelect();
			initTable();
		} else {
		}
	});
}

//续时
function consump() {
	var data_sn = getUrlParam("sn");
	var host_allowed = $('.host_allowed').val();
	if(!host_allowed) {
		$('#alertdiv').savehandle('setmsg',{msg:'请输入主机数！',timer:5000});
		return;
	}
	if(host_allowed < 1) {
		$('#alertdiv').savehandle('setmsg',{msg:'主机数必须大于0！',timer:5000});
		return;
	}
//	if(host_allowed > 600){
//		$('#alertdiv').savehandle('setmsg',{msg:'主机数不能超过600，有疑问请联系官方！',timer:5000});
//		return;
//	}
	var time = $('.timeLong').val();
	if(time == 0){
		$('#alertdiv').savehandle('setmsg',{msg:'请先选择续时时长',timer:5000});
		return;
	}
	var varCode = $('.varCode').val();
	if(!varCode){
		$('#alertdiv').savehandle('setmsg',{msg:'请输入验证码！',timer:5000});
		return;
	}
    var data = {
		'ifwAgentDevInfo.sn':data_sn,
		'host': host_allowed,
	    'time': time,
	    'vcode':varCode
    };
    $.getJSON('/fw/agent/agentCon_addConsume.action', data,function(data){
		if(data.success){
			if(data.result == "1"){
				$('#alertdiv').savehandle('setmsg',{msg:'续时成功',timer:2000});
				setTimeout(function(){
					location.href = "recOfConsList.jsp";
				}, 2000)
			}else {
				$('#alertdiv').savehandle('setmsg',{msg:'续时失败,请确保剩余点数足够',timer:5000});
			}
		}else{
			$('#alertdiv').savehandle('setmsg',{msg:data.result.errors?data.result.errors:'续时失败',timer:5000});
			return;
		}
	});
}
function changeVc(){
	$('.vcimg').attr('src','/fw/common/verify_graphCode.action?_dc' + Math.random());
}
